//
//  IRTextViewGestureRecognizer.m
//  us
//
//  Created by lhx on 2025/7/17.
//

#import "IRTextViewGestureRecognizer.h"

#import <UIKit/UIGestureRecognizerSubclass.h>

@interface IRTextViewGestureRecognizer ()
@property (nonatomic, strong) UIView *anchorView;
@property (nonatomic, assign) CGPoint initialPoint;
@property (nonatomic, assign) CGPoint anchorPoint;
@end

@implementation IRTextViewGestureRecognizer

- (instancetype)initWithTarget:(id)target action:(SEL)action anchorView:(UIView *)anchorView {
    if (self = [super initWithTarget:target action:action]) {
        _anchorView = anchorView;
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (touches.count != 1) {
        self.state = UIGestureRecognizerStateFailed;
        return;
    }
    
    self.state = UIGestureRecognizerStatePossible;
    
    UITouch *touch = [touches anyObject];
    UIView *superview = self.anchorView.superview;
    self.initialPoint = [touch locationInView:superview];
    
    // 左上角作为锚点
    self.anchorPoint = [superview convertPoint:CGPointZero fromView:self.anchorView];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    self.previousTouchPoint = [touch previousLocationInView:self.view.superview];
    self.currentTouchPoint = [touch locationInView:self.view.superview];

    self.state = UIGestureRecognizerStateChanged;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (self.state == UIGestureRecognizerStateChanged || self.state == UIGestureRecognizerStateBegan) {
        self.state = UIGestureRecognizerStateEnded;
    } else {
        self.state = UIGestureRecognizerStateFailed;
    }
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.state = UIGestureRecognizerStateFailed;
}

 
@end
